package edu.jamestang.yeb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.jamestang.yeb.entity.Department;
import edu.jamestang.yeb.mapper.DepartmentMapper;
import edu.jamestang.yeb.service.DepartmentService;
import edu.jamestang.yeb.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author JamesTang
 * @date 2021/12/16 1:51
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }

    @Override
    public Resp addDep(Department department) {

        department.setEnabled(true);
        departmentMapper.addDep(department);
        if(1 == department.getResult()){
            return Resp.success("Add successful",department);
        }
        return Resp.error("Add error");
    }

    @Override
    public Resp deleteDep(Integer id) {

        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteDep(department);

        if(-2 == department.getResult()){
            return Resp.error("This Department has children,delete fail");
        }else if(-1 == department.getResult()){
            return Resp.error("This department has employee,delete fail");
        }else if (1 == department.getResult()){
            return Resp.success("Delete successful");
        }

        return Resp.error("Delete fail");
    }
}
